package chapter19;

import java.util.concurrent.Semaphore;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/24 20:50
 */
public class AccessControlService {
    private static final int MAX_PERMITS = 100;
    private Semaphore semaphore = new Semaphore(MAX_PERMITS, true);

    public boolean login(String user, String password) {
        if (!semaphore.tryAcquire()) {
            //访问数量超过了限制
            throw new ConcurrentLimitedException();
        }
        //其他验证...
        return true;
    }

    public void logout(String name) {
        semaphore.release();
    }

    public static class ConcurrentLimitedException extends RuntimeException {
        private static final long serialVersionUID = 234184907823315728L;
    }
}
